/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pfc.utils;

import java.beans.PropertyDescriptor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.io.DatabaseIO;
import org.apache.ddlutils.model.Database;
import org.xml.sax.SAXException;
import pfc.modelocg.CGModel;
import pfc.modeloer.ERElement;
import pfc.modeloer.ERModel;
import pfc.modeloer.ERModelIO;

/**
 *
 * @author xandru
 */
public class Utils {

    public static Database getDatabaseModel(PropertiesConfiguration conf) {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName((String) conf.getProperty("driver"));
        ds.setUsername((String) conf.getProperty("usuario"));
        ds.setPassword((String) conf.getProperty("password"));
        ds.setUrl((String) conf.getProperty("url"));

        Platform platform = PlatformFactory.createNewPlatformInstance(ds);
        Database dbModel = platform.readModelFromDatabase("model");
        return dbModel;
    }

    public static Platform getDatabasePlatform(PropertiesConfiguration conf) {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName((String) conf.getProperty("driver"));
        ds.setUsername((String) conf.getProperty("usuario"));
        ds.setPassword((String) conf.getProperty("password"));
        ds.setUrl((String) conf.getProperty("url"));

        Platform platform = PlatformFactory.createNewPlatformInstance(ds);
        return platform;
    }

    public static void modelToXML(Database db, String filepath)
    {
        new DatabaseIO().write(db, filepath);
    }

    public static Database XMLtoModel(String filepath) {
        Database db = new DatabaseIO().read(filepath);
        return db;
    }

    public static void modelToXML(ERModel er, String dsFile) {
        try {
            new ERModelIO().doERModel2XML(er, dsFile);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex);
        }
    }

    public static ERModel XMLToERModel(String entityModelFile){
         ERModel er = null;
          er = new ERModelIO().doXML2ERModel(entityModelFile);
      
         return er;
        }
    


    public static Object propertyValue(Object bean, String propiedad, String valor) {
        PropertyDescriptor pd = null;
        try {
            pd = PropertyUtils.getPropertyDescriptor(bean, propiedad);
        } catch (Exception ex) {
            System.out.println("OPPPPPPPPPPPPPPPPPPPPPPPPPPPPPSSSSSSSS " + ex.getMessage());
            Logger.getLogger(ERModelIO.class.getName()).log(Level.SEVERE, null, ex);
        }

        //System.out.println(bean + "." + propiedad + "(" + pd.getPropertyType().getSimpleName() + ") [" + valor + "]");
        if (pd.getPropertyType().getSimpleName().equals("EntityType"))
        {
          return ERElement.EntityType.valueOf(valor);
        }

        if (pd.getPropertyType().getSimpleName().equals("String")) {
            return valor;
        }

        if (pd.getPropertyType().getSimpleName().equals("boolean")) {
            if (valor.equals("true")) {
                return true;
            } else {
                return false;
            }
        }

        if (pd.getPropertyType().getSimpleName().equals("int")) {
            if ((valor.equals(""))) {
                return 0;
            }
            return Integer.parseInt(valor);
        }

        return "";
    }

    public static String propertyValueObj(Object bean, String s, Object property) {
        PropertyDescriptor pd = null;
        try {
            pd = PropertyUtils.getPropertyDescriptor(bean, s);
        } catch (Exception ex) {
            System.out.println("OPPPPPPPPPPPPPPPPPPPPPPPPPPPPPSSSSSSSS " + ex.getMessage());
            Logger.getLogger(ERModelIO.class.getName()).log(Level.SEVERE, null, ex);
        }

        //System.out.println(bean + "." + s + "(" + pd.getPropertyType().getSimpleName() + ") [" + property + "]");

        if (property == null) {
            return "";
        }

        //System.out.println("->" + pd.getPropertyType().getSimpleName());
        if (pd.getPropertyType().getSimpleName().equals("EntityType"))
        {
          return property.toString();
        }

        if (pd.getPropertyType().getSimpleName().equals("String")) {
            return property.toString();
        }

        if (pd.getPropertyType().getSimpleName().equals("boolean")) {
            Boolean b = (Boolean) property;
            return Boolean.toString(b);
        }

        if (pd.getPropertyType().getSimpleName().equals("int")) {
            Integer i = (Integer) property;
            return i.toString();
        }



        return "";
    }

    public static String getTipoJava(int tipoSQL)
    {
        switch (tipoSQL)
        {
                case java.sql.Types.BIT:return "Boolean";
                case java.sql.Types.TINYINT:return "Boolean";
                case java.sql.Types.SMALLINT:return "Short";
                case java.sql.Types.INTEGER:return "Integer";
                case java.sql.Types.BIGINT:return "Long";
                case java.sql.Types.REAL:return "Float";
                case java.sql.Types.FLOAT:return "Double";
                case java.sql.Types.DOUBLE:return "Double";
                case java.sql.Types.DATE:return "Date";
                case java.sql.Types.TIME:return "Time";
                case java.sql.Types.TIMESTAMP:return "Timestamp";
                case java.sql.Types.VARCHAR:return "String";
                case java.sql.Types.BLOB:return "Blob";
        }
        return "String";
    }
}


